<div id="rbac_resource_index">

    <div class="btn-toolbar m-b">
        <button class="btn btn-default" type="button" @click="add">新增</button>
    </div>

    <datatable
        :ctx="this"
        ref="datatable"
        index="id"
        request="rbac/Resource/index"
        :fields="fileds"
        header="<th>上级资源ID</th><th>类型</th><th>链接地址</th><th>标识符</th><th>资源名称</th><th style='width:100px'>操作</th>"
    >
        <div slot="oprate">
            <span @click="edit">修改</span> |
            <span @click="del">删除</span>
        </div>
    </datatable>

</div>

<script>
    new Vue({
        el:'#rbac_resource_index',
        data:{
            fileds:[
                {name:'pid',callback:function (v) {
                    if(v  == 0){
                        return  '顶级资源' ;
                    }
                    for(var i = 0 ; i < this.allRes.length ; i++ ){
                        if(this.allRes[i].id == v){
                            return this.allRes[i].name
                        }
                    }
                    return v
                }},
                {name:'type',callback:function (v) {
                    return v == 0 ? '菜单' : '按钮'
                }},
                'link',
                'code',
                'name'
            ],
            allRes:[],
        },
        mounted:function () {
            var _this = this ;
            zhttp.get('rbac/resource/all').then(function (data) {
                _this.allRes = data ;
            })
        },
        methods:{
            _getId:function (e) {
                return $(e.target).parents('tr').data('id') ;
            },
            add:function(){
                go('rbac/Resource/save')
            },
            edit:function(e){
                go('rbac/Resource/save?id='+this._getId(e))
            },
            del:function(e){
                var id = this._getId(e) ;
                var _this = this ;
                bootbox.confirm("确定要删除？", function(ok){
                    if(!ok){ return }
                    $.get('rbac/Resource/delete',{id:id},function(d){
                        if(d.code == 0){
                            success("操作成功")
                            _this.$refs.datatable.refresh()
                        }
                    })
                });
            },
        }
    })
</script>
<script>

</script>